<?php
namespace Admin\Api;

use Admin\Model\FreightAddressModel;
use Admin\Model\FreightTemplateModel;

/**
 * Created by PhpStorm.
 * User: an
 * Date: 2015/8/13
 * Time: 15:59
 */
use \Common\Api\Api;
use Admin\Api\FreightAddressApi;

class FreightTemplateApi extends Api{



    public function _init() {
        $this->model = new FreightTemplateModel();
    }

    const QUERY_NO_PAGING="Admin/FreightTemplate/queryNoPaging";

    const QUERY="Admin/FreightTemplate/query";

    const ADD="Admin/FreightTemplate/add";

    const QUERY_WITH_UID = "Admin/FreightTemplate/queryWidthUID";

    const GET_INFO = "Admin/FreightTemplate/getInfo";

    const ADD_TEMPLATE = "Admin/FreightTemplate/addTemplate";

    const UPDATE_TEMPLATE = "Admin/FreightTemplate/updateTemplate";

    const DELETE_TEMPLATE = "Admin/FreightTemplate/deleteTemplate";

    const FIND_TEMPLATE = "Admin/FreightTemplate/findTemplate";

    /**
     * 根据用户ID查询运费模版
     */
    public function queryWidthUID($uid = -1, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false){

        $query = $this -> model;

        $map =array('uid' => $uid);

        $query = $query -> relation(true) -> where($map);

        if(!($order === false)){
            $query = $query -> order($order);
        }

        $list = $query->select();

        if($list === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));

    }

    /**
     * 查询指定运费模版
     */
    public function findTemplate($map){

        $model = $this -> model;

        $result = $model->where($map)->relation(true)->find();

        if ($result === false) {
            return $this -> apiReturnErr($this -> model -> getDbError());
        }

        return $this -> apiReturnSuc($result);
    }

    /**
     * 添加运费模版
     */
    public function addTemplate($entity){

        $model = $this -> model;

        $result = $model->relation(true)->add($entity);

        if ($result === false) {
            return $this -> apiReturnErr($this -> model -> getDbError());
        }

        return $this -> apiReturnSuc($result);

    }
    /**
     * 更新运费模版
     */
    public function updateTemplate($map,$entity){

        //删除旧模版地址数据

        $m = new FreightAddressModel();
        $m->startTrans();
        if(isset($map['id'])){

            $result = $m->where(array('template_id'=>$map['id']))->delete();
            if ($result === false) {
                $m->rollback();
                return $this -> apiReturnErr($this -> model -> getDbError());
            }

            $m->commit();

        }

        $model = $this -> model;

        $result = $model->relation(true)->where($map)->save($entity);

        if ($result === false) {
            return $this -> apiReturnErr($this -> model -> getDbError());
        }

        return $this -> apiReturnSuc($result);

    }

    /**
     * 删除运费模版
     */
    public function deleteTemplate($map){

        $result = $this->model->where($map)->relation(true)->delete();

        if ($result === false) {
            return $this -> apiReturnErr($this -> model -> getDbError());
        }

        return $this -> apiReturnSuc($result);

    }


}